using System.Collections;
using UnityEngine;

public class CollisionControl : MonoBehaviour
{
	public string key;

	public PlayerMovement movement;

	public FileSetup SETUP;

	private SpriteRenderer sprMain;

	public bool HidenBody;

	private HingeJoint2D hingeJoint2D;

	public float maxForce;

	public SpriteRenderer sprStatus;

	public SpriteRenderer sprHit;

	private Color32 colorStatus;

	public bool isSlow;

	public float[] angleNormal = new float[2];

	public float[] angleOnJump = new float[2];

	private float[] angleIdleWithVehicle = new float[2];

	private bool withVehicles;

	private bool intro;

	private Color colorBase;

	private float totalTemp;

	private JointAngleLimits2D angleLimit;

	private bool changeLimited;

	private bool showbone;

	private bool blockdame;

	private float x;

	private float y;

	private bool isShowbomeAll;

	private void Awake()
	{
		hingeJoint2D = GetComponent<HingeJoint2D>();
		movement = GetComponentInParent<PlayerMovement>();
		withVehicles = movement.withVehicles;
		if (base.name == "spine")
		{
			angleNormal[1] += 15f;
		}
		else if (base.name == "spine (1)")
		{
			angleNormal[1] += 20f;
		}
		else if (base.name == "spine (2)")
		{
			angleNormal[1] += 30f;
		}
		if (!withVehicles)
		{
			_setAngleNormal();
			return;
		}
		float jointAngle = hingeJoint2D.jointAngle;
		angleIdleWithVehicle[0] = jointAngle - 10f;
		angleIdleWithVehicle[1] = jointAngle + 10f;
		SetLimitStartWithVehicles();
	}

	private void SetLimitStartWithVehicles()
	{
		angleLimit.min = angleIdleWithVehicle[0];
		angleLimit.max = angleIdleWithVehicle[1];
		hingeJoint2D.limits = angleLimit;
	}

	private IEnumerator delay_changeRotation()
	{
		yield return new WaitForSeconds(0.25f);
		hingeJoint2D.anchor = base.transform.InverseTransformPoint(GetComponent<LegFix>().AnchorHingeJoint2d.position);
	}

	public void _setAngleNormal()
	{
		angleLimit.min = angleNormal[0];
		angleLimit.max = angleNormal[1];
		hingeJoint2D.limits = angleLimit;
	}

	public void _setAngleOnJump()
	{
		angleLimit.min = angleOnJump[0];
		angleLimit.max = angleOnJump[1];
		hingeJoint2D.limits = angleLimit;
	}

	private void Start()
	{
		sprMain = GetComponent<SpriteRenderer>();
		if (!HidenBody)
		{
			colorBase = SETUP.colorNormal[0];
		}
		else
		{
			colorBase = SETUP.colorNormal[1];
		}
		sprMain.color = colorBase;
		colorStatus = sprStatus.color;
		colorStatus.a = 0;
		sprHit.enabled = false;
		sprStatus.color = colorStatus;
		sprStatus.enabled = true;
		if (base.name == "head")
		{
			base.transform.Find("spine").GetComponent<SpriteRenderer>().color = colorBase;
		}
		intro = ((!(GUIManager.Instance != null)) ? true : false);
		
	}

	private void Update()
	{
		totalTemp = hingeJoint2D.reactionForce.magnitude;

	}
	public void _TakeDame()
	{
		if (!movement.dame)
		{
			return;
		}
		totalTemp = 0f;
		if (blockdame)
		{
			return;
		}
		blockdame = true;
		if (totalTemp < maxForce)
		{
			colorStatus.a = (byte)(totalTemp / maxForce * 255f);
			if (totalTemp > 200f && !intro)
			{
				ScoreManager.Instance.SetScore((int)(totalTemp * 0.1f));
			}
			if (!(totalTemp > 600f))
			{
			}
		}
		else
		{
			colorStatus.a = byte.MaxValue;
			if (!intro)
			{
				ScoreManager.Instance.SetScore((int)(totalTemp * 0.2f));
			}
		}
		showbone = ((colorStatus.a >= 220) ? true : false);
		_switchColor();
		movement._takeDameALL();
	}

	private void OnCollisionEnter2D(Collision2D coll)
	{
		//Debug.Log(coll.gameObject.GetComponent<Rigidbody2D>().velocity + "::" + coll.gameObject.name);
		if (!movement.dame)
		{
			return;
		}
		totalTemp = 0f;
		if (!(coll.gameObject.layer.ToString() != "Player") || blockdame)
		{
			return;
		}
		blockdame = true;
		totalTemp = hingeJoint2D.reactionForce.magnitude;
		if (totalTemp < maxForce)
		{
			colorStatus.a = (byte)(totalTemp / maxForce * 255f);
			if (totalTemp > 200f && !intro)
			{
				ScoreManager.Instance.SetScore((int)(totalTemp * 0.1f));
			}
		}
		else
		{
			colorStatus.a = byte.MaxValue;
			if (!intro)
			{
				ScoreManager.Instance.SetScore((int)(totalTemp * 0.2f));
			}
		}
		if (totalTemp > 1000000f)
		{
			movement.showBoneALL();
		}
		showbone = ((colorStatus.a >= 220) ? true : false);
		if (colorStatus.a >= 180)
		{
			if (!intro)
			{
				DustManager.Instance.playBlood(coll.contacts[0].point);
			}
			else
			{
				DustManager.Instance.playBlood(base.transform.position);
			}
		}
		if (colorStatus.a >= 120 && coll.transform.tag == "PLATFORM")
		{
			if (!intro)
			{
				DustManager.Instance.play(coll.contacts[0].point);
				SoundScript.Instance._PlayClipHitBody();
			}
			else
			{
				DustManager.Instance.playBlood(base.transform.position);
			}
		}
		_switchColor();
	}

	public void _switchColor()
	{
		if (!isShowbomeAll)
		{
			StartCoroutine(delay_switchColor());
		}
	}

	private IEnumerator delay_switchColor()
	{
		if (showbone)
		{
			_setAngleNormal();
			movement.countTimeBone++;
			if (!intro)
			{
				SlowMotionManager.Instance._SlowOnHit();
				SoundScript.Instance._PlayClipBoneBreak();
			}
		}
		sprStatus.color = colorStatus;
		if (showbone)
		{
			sprHit.enabled = true;
		}
		yield return new WaitForSeconds(0.1f);
		colorStatus.a = (byte)((int)colorStatus.a / 2);
		sprStatus.color = colorStatus;
		yield return new WaitForSeconds(0.1f);
		colorStatus.a = 0;
		sprStatus.color = colorStatus;
		if (showbone)
		{
			sprHit.enabled = false;
			showbone = false;
		}
		blockdame = false;
	}

	public void showBoneAll()
	{
		isShowbomeAll = true;
		blockdame = true;
		sprHit.enabled = true;
		colorStatus.a = byte.MaxValue;
		sprStatus.color = colorStatus;
		StartCoroutine(HideBone());
	}

	private void OnCollisionStay2D(Collision2D coll)
	{
		if (coll.gameObject.layer.ToString() == "VEHICLES" && !intro)
		{
			PlayerControlManager.Instance.OutVehicle();
		}
	}

	private IEnumerator HideBone()
	{
		yield return new WaitForSeconds(1f);
		blockdame = false;
		sprHit.enabled = false;
		colorStatus.a = 0;
		sprStatus.color = colorStatus;
		isShowbomeAll = false;
		if (movement.isShowBoneall)
		{
			movement.isShowBoneall = false;
		}
	}
}
